﻿using App.Metrics;
using App.Metrics.Extensions.Configuration;
using App.Metrics.Filtering;
using App.Metrics.Formatters.Json;
using App.Metrics.Health;
using App.Metrics.Health.Extensions.Configuration;
using App.Metrics.Health.Formatters.Ascii;
using App.Metrics.Reporting.InfluxDB;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using App.Metrics.Health.Checks.Sql;
using Chaunce.FrameWork.NetCore.SysCore.Configuration;
using Chaunce.FrameWork.NetCore.SysCore.Infrastructure;

namespace Chaunce.Web.Core.Infrastructure
{
    /// <summary>
    /// 应用性能监控
    /// </summary>
    public class AppMetricsStartup : IChaunceStartup
    {
        public int Order => 200;

        public void Configure(IApplicationBuilder application)
        {
            var config = EngineContext.Current.Resolve<ChaunceConfig>();
            if (!config.AMPEnabled) return;
            application.UseMetricsAllMiddleware();
            application.UseMetricsAllEndpoints();
            //健康检测
            application.UseHealthAllEndpoints();
        }

        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            var config = services.BuildServiceProvider().GetRequiredService<ChaunceConfig>();
            if (!config.AMPEnabled) return;

            var connectionString = config.ConnectionStrings?.DecryptDefaultConnection;
            var influxOptions = new MetricsReportingInfluxDbOptions();
            configuration.GetSection(nameof(MetricsReportingInfluxDbOptions)).Bind(influxOptions);

           var metricsRoot= AppMetrics.CreateDefaultBuilder()
                .Configuration.ReadFrom(configuration)
                .Report.ToInfluxDb(influxOptions)
                .Build();
         
            services.AddMetrics(metricsRoot);
            services.AddMetricsReportScheduler();
            services.AddMetricsTrackingMiddleware(configuration);
            services.AddMetricsEndpoints(configuration);

            //健康检测
            var metricsHealth = AppMetricsHealth.CreateDefaultBuilder()
             .Configuration.ReadFrom(configuration)
            .HealthChecks.RegisterFromAssembly(services)
            .HealthChecks.AddPingCheck("ping 百度", "www.baidu.com", TimeSpan.FromSeconds(5))
            .HealthChecks.AddHttpGetCheck("外教官网", new Uri("http://wwww.Chaunce.com.cn"), TimeSpan.FromSeconds(30))
            .HealthChecks.AddHttpGetCheck("外教后台", new Uri("http://admin.Chaunce.com.cn"), TimeSpan.FromSeconds(30))
            .HealthChecks.AddProcessPhysicalMemoryCheck("占用内存是否超过1G", 1024*1024*1024)
            .HealthChecks.AddSqlCheck("数据库连接检测", connectionString, TimeSpan.FromSeconds(60))
            .BuildAndAddTo(services);

            services.AddHealth(metricsHealth);
            services.AddHealthEndpoints(configuration);

        }
    }
}
